package com.itheima.reggie.controller;

import cn.hutool.db.Session;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.damain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/employee/login")
    public ResultInfo login(HttpSession session, @RequestBody Map<String,String>map) {
        //接收参数
        String username = map.get("username");
        String password = map.get("password");

        //调用service登录
        ResultInfo resultInfo = employeeService.login(username, password);

        //保存信息到session
        if (resultInfo.getCode() == 1) {
            Employee employee = (Employee) resultInfo.getData();
            session.setAttribute("STATUS_ENABLE", employee);
        }
        //返回结果
        return resultInfo;
    }
    @GetMapping("/employee/find")
    public ResultInfo find(String  name){

        //调用service查询
        List<Employee>employeeList=employeeService.findbyname(name);
        //结果返回
        return ResultInfo.success(employeeList);
    }
    @PostMapping("/employee/logout")
    public ResultInfo logout(HttpSession session){
        //注销session
        session.invalidate();
        //返回成功标识
        return ResultInfo.success(null);

    }
    //保存
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee){
        employeeService.save(employee);
        return ResultInfo.success(null);

    }
    //按id查询
    @GetMapping("/employee/{id}")
    public ResultInfo findbyid(@PathVariable("id") long id){
        Employee employee=employeeService.fingbyid(id);
        return ResultInfo.success(employee);
    }
    //更新
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success(null);

    }




}
